package com.ima.web.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ima.web.beans.db.Categories;
import com.ima.web.beans.db.SubCategories;
import com.ima.web.beans.db.Users;
import com.ima.web.beans.form.SubCategoriesForm;
import com.ima.web.dao.SubCategoriesDao;
import com.ima.web.service.CategoriesService;
import com.ima.web.service.SubCategoriesService;
import com.ima.web.service.UserService;

@Service
@Transactional
public class SubCategoriesServiceImpl implements SubCategoriesService {
	private SubCategoriesDao subCatDao;
	private UserService userService;
	private CategoriesService categoriesService;
	
	@Autowired
	public void setDepencies(SubCategoriesDao subCatDao, 
						     UserService userService,
						     CategoriesService categoriesService){
		this.subCatDao = subCatDao;
		this.userService = userService;
		this.categoriesService = categoriesService;
		
	}
	
	@Override
	public List<SubCategories> getByCategory(String category) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SubCategories> getByCategory(Categories cat) {
		SubCategories sc = new SubCategories();
		
		sc.setApproved(true);
		sc.setCategory(cat);
		sc.setDeleted(false);
		
		return subCatDao.list(sc);
	}

	@Override
	public List<SubCategories> getByCategoriesId(int id) {
		SubCategories sc = new SubCategories();
		
		sc.setApproved(true);
		sc.setCategory(categoriesService.getById(id));
		sc.setDeleted(false);
		
		return subCatDao.list(sc);
	}

	@Override
	public List<SubCategories> getByName(String subcategory) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SubCategories> getApprovedBy(String approverUsername) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SubCategories> getApprovedBy(int userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SubCategories> getApprovedBy(Users user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void insert(SubCategories subCategory) {
		subCatDao.save(subCategory);

	}

	@Override
	public List<SubCategories> getForProcessing() {
		return subCatDao.getToBeApproved();
	}

	@Override
	public SubCategories getById(int id) {
		return subCatDao.findById(id);
	}

	@Override
	public void insert(SubCategoriesForm formData) {
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		Users u = userService.getUserData(username);
		
		Date now = new Date();
		
		SubCategories sc = new SubCategories();
		Categories c = categoriesService.getById(formData.getCategoriesId());
		
		//No categories id, it's a new category
		if(formData.getSubCategoriesId() == null){
			sc.setCreatedDate(now);
			
			//set requestor, if its new - requester should be included
			sc.setRequester(u);
			sc.setRequestedDate(now);
		}else{
			sc = getById(formData.getSubCategoriesId());
		}
		
		sc.setSubCategoriesId(formData.getSubCategoriesId());
		sc.setName(formData.getName());
		sc.setDescription(formData.getDescription());
		sc.setCategory(c);
				
		//Only admin has direct approval
		if(formData.getApproved()){	
			sc.setApproved(true);
			sc.setApprovedDate(now);
			sc.setApprover(u);
		}else{
			sc.setApproved(false);
			sc.setApprover(null);
			sc.setApprovedDate(null);
		}
		
		sc.setUpdatedDate(now);
		
		subCatDao.save(sc);
	}

	@Override
	public void approve(int subCategoriesId, boolean isApproved) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void approve(SubCategoriesForm formData) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void approve(SubCategoriesForm subCategory, boolean isApproved) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<SubCategories> getApproved() {
		SubCategories sc = new SubCategories();
		sc.setApproved(true);
		sc.setDeleted(false);
		
		return subCatDao.list(sc);
	}

	@Override
	public List<SubCategoriesForm> getListForForm() {
		List<SubCategories> sc = getApproved();
		List<SubCategoriesForm> scf = new ArrayList<SubCategoriesForm>();
		
		for(SubCategories scNS : sc){
			SubCategoriesForm scNew = new SubCategoriesForm();
			
			scNew.setApproved(true);
			scNew.setCategoriesId(scNS.getCategory().getCategoriesId());
			scNew.setDescription(scNS.getDescription());
			scNew.setName(scNS.getName());
			scNew.setSubCategoriesId(scNS.getSubCategoriesId());
			
			scf.add(scNew);
		}
		
		return scf;
	}

	@Override
	public List<SubCategoriesForm> getListForFormForCat(int catId) {
		List<SubCategories> sc = getByCategoriesId(catId);
		List<SubCategoriesForm> scf = new ArrayList<SubCategoriesForm>();
		
		for(SubCategories scNS : sc){
			SubCategoriesForm scNew = new SubCategoriesForm();
			
			scNew.setApproved(true);
			scNew.setCategoriesId(scNS.getCategory().getCategoriesId());
			scNew.setDescription(scNS.getDescription());
			scNew.setName(scNS.getName());
			scNew.setSubCategoriesId(scNS.getSubCategoriesId());
			
			scf.add(scNew);
		}
		
		return scf;
	}

}
